package phoeics.authoryserver;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import phoeics.authoryserver.webservice.security.RedisClientDetailsServiceBuilder;
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter{
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AuthorizationServerConfiguration.class);
	private static String REALM="MY_OAUTH_REALM";
	/*@Autowired
	private TokenStore tokenStore;*/
	/*@Autowired
	private UserApprovalHandler userApprovalHandler;*/
	
	@Resource(name="authenticationManagerBean")
	private AuthenticationManager authenticationManager;
	
	@Autowired
	RedisClientDetailsServiceBuilder redisClientDetailsServiceBuilder;
	 @Bean
     public JwtAccessTokenConverter accessTokenConverter() {
         JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
         converter.setSigningKey("123");
         return converter;
     }
	
	 @Bean
	    public TokenStore tokenStore() {
		 return new JwtTokenStore(accessTokenConverter());
	 }
	
	@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.setBuilder(redisClientDetailsServiceBuilder);
	}
	@Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.tokenStore(tokenStore()).accessTokenConverter(accessTokenConverter())
				.authenticationManager(authenticationManager);
	}
	 @Bean
     @Primary
     public DefaultTokenServices tokenServices() {
         DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
         defaultTokenServices.setTokenStore(tokenStore());
         defaultTokenServices.setSupportRefreshToken(true);
         return defaultTokenServices;
     }
	@Override
	public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
		oauthServer.realm(REALM+"/client");
	}
	
	
}
